package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingPlanPageQueryDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 分页查询护理计划
     *
     * @param dto
     */
    @Override
    public PageBean<NursingPlanVo> pageQuery(NursingPlanPageQueryDto dto) {
        // 使用 PageHelper 分页插件
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<NursingPlan> page = nursingPlanMapper.pageQuery(dto);// 获取所有护理计划
        return PageBean.of(page, NursingPlanVo.class);
    }

    /**
     * 增加护理计划
     *
     * @param dto
     */
    @Override
    @Transactional
    public void addNursingPlan(NursingPlanDto dto) {
        //将dto转换为pojo
        NursingPlan plan = BeanUtil.toBean(dto, NursingPlan.class);
        plan.setStatus(1); //默认计划状态 1
        //添加护理计划数据
        nursingPlanMapper.insert(plan); //必须要做主键自增返回

        //如果集合为空，不用遍历
        if (CollUtil.isEmpty(dto.getProjectPlans())) {
            return;
        }

        //遍历dto中的护理项目关系集合
        dto.getProjectPlans().forEach(planDto -> {
            planDto.setPlanId(plan.getId());
            //将dto转换为pojo
            NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(planDto, NursingProjectPlan.class);
            //添加护理计划和护理项目关系
            nursingProjectPlanMapper.insert(nursingProjectPlan);
        });
    }

    /**
     * 根据id查询护理计划
     *
     * @param id
     */
    @Override
    public NursingPlanVo getDetailById(Long id) {
        //1.先查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        //2.将pojo中的数据封装到vo中
        NursingPlanVo vo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        //3.根据计划id查询对应的护理项目
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlanMapper.getByPlanId(id);
        vo.setProjectPlans(nursingProjectPlanVos);
        return vo;
    }

    /**
     * 更新护理计划
     *
     * @param dto
     */
    @Override
    @Transactional
    public void updateNursingPlan(NursingPlanDto dto) {
        NursingPlan plan = BeanUtil.toBean(dto, NursingPlan.class);

        //1. 根据计划id删除原来的关联数据
        nursingProjectPlanMapper.deleteByPlanId(plan.getId());

        if (CollUtil.isNotEmpty(dto.getProjectPlans())) {
            dto.getProjectPlans().forEach(planDto -> {
                planDto.setPlanId(plan.getId());
                NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(planDto, NursingProjectPlan.class);
                //2. 重新添加护理计划，护理项目关系
                nursingProjectPlanMapper.insert(nursingProjectPlan);
            });
        }
        //3. 更新护理计划信息
        nursingPlanMapper.updateNursingPlan(plan);
    }

    /**
     * 根据id删除护理计划
     *
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Long id) {
        //删除关联关系
        nursingProjectPlanMapper.deleteByPlanId(id);
        //删除护理计划
        nursingPlanMapper.deleteById(id);
    }

    /**
     * 查询所有护理计划
     */
    @Override
    public List<NursingPlanVo> getAll() {
        return nursingPlanMapper.getAll();
    }


    /**
     * 启用或禁用
     *
     * @param id     ID
     * @param status 状态
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        NursingPlan nursingPlan = NursingPlan.builder().id(id).status(status).build();
        nursingPlanMapper.updateNursingPlan(nursingPlan);
    }
}
